<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.class" placeholder="全部班级" clearable style="width: 120px" class="filter-item" size="mini" round @change="handleFilter">
        <el-option v-for="item in classoption" :key="item" :label="item" :value="item" />
      </el-select>
      <el-input v-model="listQuery.title" placeholder="请输入关键字" style="width: 200px;margin-left: 10px" class="filter-item" size="mini" round @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" size="mini" style="margin-left: 10px" round @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" size="mini" style="margin-left: 10px;float:right" @click="go">
        添加
      </el-button>
      <el-button class="filter-item" size="mini" style="margin-left: 10px;float:right" @click.prevent="delData">
        删除
      </el-button>
    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      stripe
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @selection-change="selectRow"
    >
      <el-table-column type="selection" width="77" />
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="{$index}">
          <span>{{ $index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年级" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gradeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.className }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理人" min-width="300px">
        <template slot-scope="{row}">
          <span>{{ row.teacherName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="medium" type="text" title="删除" @click="handleDelete(row)"><i class="el-icon-delete" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { getclassTeacherList, deleteclassTeacherList, deleteclassTeacherLists } from '@/api/classSetting'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      sel: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        // title: undefined,
        gradeName: '',
        keyword: '',
        teacherId: ''
      },
      classoption: [],
      temp: {
        item: [],
        asTeacherclassId: '',
        classId: '',
        gradeId: '',
        teacherId: '',
        className: '',
        gradeName: '',
        teacherName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        // class: [{ required: true, message: 'class is required', trigger: 'blur' }],
        // grade: [{ required: true, message: 'grade is required', trigger: 'blur' }],
        // person: [{ required: true, message: 'person is required', trigger: 'blur' }],
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    go() { this.$router.push('/setting/index/5/classTeacher') },
    handleDelete(row) {
      console.log(row)
      this.$confirm(
        '确定要删除吗？删除后不能恢复',
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        deleteclassTeacherList(row)
        this.getList()
        this.$notify({
          title: 'Success',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    selectRow(sels) {
      this.sels = sels
    },
    // 批量删除
    delData() {
      this.$confirm(
        '确定要删除吗？删除后不能恢复',
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        for (let i = 0; i < this.sels.length; i++) {
          this.temp.item[i] = this.sels[i].asTeacherclassId
        }
        deleteclassTeacherLists(this.temp)
        this.getList()
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
        // this.getList()
      })
    },
    getList() {
      this.listLoading = true
      this.listQuery.teacherId = 'fxz'
      getclassTeacherList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    resetTemp() {
      this.temp = {
        num: '',
        class: '',
        grade: '',
        person: ''
      }
    }

  }
}
</script>
